﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;
using AutoMapper;

namespace Bot.Domain.Categories.Service
{
    public class AnswerInChannelAuthenticationRequestDomainService : IAnswerInChannelAuthenticationRequestDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, AnswerInChannelAuthenticationRequest> _repository;
        public IMapper Mapper { get; set; }

        public AnswerInChannelAuthenticationRequestDomainService(IRepository<Guid, AnswerInChannelAuthenticationRequest> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public AnswerInChannelAuthenticationRequest Create(AnswerInChannelAuthenticationRequest answerInChannelAuthenticationRequest)
        {
            if (answerInChannelAuthenticationRequest.Id == Guid.Empty)
            {
                answerInChannelAuthenticationRequest.Id = Guid.NewGuid();
            }
            return _repository.Create(answerInChannelAuthenticationRequest);
        }

        public AnswerInChannelAuthenticationRequest Get(Guid id)
        {
            return _repository.Get(id);
        }

        public AnswerInChannelAuthenticationRequest Update(AnswerInChannelAuthenticationRequestUpdateBo bo)
        {
            AnswerInChannelAuthenticationRequest answerInChannelAuthenticationRequest = _repository.Get(bo.Id);
            answerInChannelAuthenticationRequest.Id = bo.Id;
            answerInChannelAuthenticationRequest.SignInMessage = bo.SignInMessage;
            answerInChannelAuthenticationRequest.SignInButtonText = bo.SignInButtonText;
            answerInChannelAuthenticationRequest.Method = bo.Method;
            answerInChannelAuthenticationRequest.SignInURL = bo.SignInURL;
            answerInChannelAuthenticationRequest.CustomVariable = bo.CustomVariable;

            return answerInChannelAuthenticationRequest;

        }

        public IEnumerable<AnswerInChannelAuthenticationRequest> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            AnswerInChannelAuthenticationRequest answerInChannelAuthenticationRequest = _repository.Get(id);
            if (answerInChannelAuthenticationRequest != null)
            {
                IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();
                var intentAnswerInChannel = intentAnswerInChannelDomainService.GetByAuthenticationRequestId(id);
                if (intentAnswerInChannel != null)
                {
                    intentAnswerInChannelDomainService.UpdateForAuthenticationRequestId(intentAnswerInChannel.Id, Guid.Empty);
                }

                _repository.Delete(answerInChannelAuthenticationRequest);
            }
        }

        public AnswerInChannelAuthenticationRequest GetByAnswerInChannelId(Guid answerInChannelId)
        {
            IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();
            var intentAnswerInChannel = intentAnswerInChannelDomainService.Get(answerInChannelId, "authenticationRequest");

            return intentAnswerInChannel.AuthenticationRequest;
        }
        public AnswerInChannelAuthenticationRequest CreateWithAnswerInChannelId(Guid answerInChannelId, AnswerInChannelAuthenticationRequest answerInChannelAuthenticationRequest)
        {
            answerInChannelAuthenticationRequest = Create(answerInChannelAuthenticationRequest);
            IIntentAnswerInChannelDomainService intentAnswerInChannelDomainService = _container.Resolve<IIntentAnswerInChannelDomainService>();
            intentAnswerInChannelDomainService.UpdateForAuthenticationRequestId(answerInChannelId, answerInChannelAuthenticationRequest.Id);
            return answerInChannelAuthenticationRequest;
        }
    }
}
